package com.das.mechanic_base.adapter.main;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.utils.X3ScreenUtils;

import java.util.List;

public class X3SelectSunNameAdapter extends RecyclerView.Adapter<X3SelectSunNameAdapter.SelectSunNameHolder> {

    private List<String> mList;

    private Context mContext;

    public X3SelectSunNameAdapter(List<String> mList, Context mContext) {
        this.mList = mList;
        this.mContext = mContext;
    }

    public void refreshSunName(List<String> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public SelectSunNameHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SelectSunNameHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_select_sunname_layout,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull SelectSunNameHolder holder, int position) {
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
        if (position == 0) {
            layoutParams.leftMargin = X3ScreenUtils.dipToPx(16,mContext);
            layoutParams.rightMargin = 0;
        }else if (position == mList.size() - 1) {
            layoutParams.leftMargin = X3ScreenUtils.dipToPx(10,mContext);
            layoutParams.rightMargin = X3ScreenUtils.dipToPx(10,mContext);
        }else {
            layoutParams.leftMargin = X3ScreenUtils.dipToPx(10,mContext);
            layoutParams.rightMargin = 0;
        }
        holder.itemView.setLayoutParams(layoutParams);

        holder.tv_name.setText(mList.get(position) + "");

        holder.itemView.setOnClickListener(v -> {
            if (iOnClickSelect != null) {
                iOnClickSelect.iOnClickSelectSunName(mList.get(position));
            }
        });
    }

    IOnClickSelect iOnClickSelect;

    public void setiOnClickSelect(IOnClickSelect iOnClickSelect) {
        this.iOnClickSelect = iOnClickSelect;
    }

    public interface IOnClickSelect {
        void iOnClickSelectSunName(String name);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class SelectSunNameHolder extends RecyclerView.ViewHolder {
        private TextView tv_name;
        public SelectSunNameHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
        }
    }
}
